home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / ARexxTools / Rxil.lha / Rxil / src / dispatch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-30  |  3.4 KB  |  170 lines

  1. /* dispatch.c */
  2.  
  3. /*        Copyright © 1989 by Donald T. Meyer, Stormgate Software
  4.  *        All Rights Reserved
  5.  */
  6.  
  7.  
  8.  
  9. #include "rxil.h"
  10.  
  11. #include <string.h>
  12.  
  13.  
  14.  
  15. /* NAME
  16.  *        RxilDispatch
  17.  *
  18.  * SYNOPSIS
  19.  *        RxilDispatch( rexxmsg, cmd );
  20.  *
  21.  *        struct RexxMsg *rexxmsg;
  22.  *        char *cmd;
  23.  *
  24.  * FUNCTION
  25.  *        This is really an "internal" function which is used by
  26.  *        the RxilCheckPort() function to dispatch commands.
  27.  *
  28.  *        It is not static, since it may be usefull by itself, or
  29.  *        it may desireable to replace it with a custom version
  30.  *        in some applications.  This can be done simply by overriding
  31.  *        it, rather than editing the library itself.
  32.  *
  33.  *        This is NOT a "safe" call, since it should never be called
  34.  *        directly from client code.
  35.  *        It is however safe if the client fails to initialize the command
  36.  *        table pointer.
  37.  *
  38.  *
  39.  * INPUTS
  40.  *        rexxmsg = pointer to the RexxMsg structure for the command we
  41.  *            are dispatching.
  42.  *        cmd = the command name string.
  43.  *
  44.  * RESULT
  45.  *        None
  46.  *
  47.  * SIDES
  48.  *
  49.  * HISTORY
  50.  *        01-Aug-89    Creation.
  51.  *        26-Sep-89    Changed to use maximum and minimum argcounts.
  52.  *
  53.  * BUGS
  54.  *
  55.  * SEE ALSO
  56.  *        RxilCheckPort()
  57.  */
  58.  
  59. void RxilDispatch( struct RexxMsg *rexxmsg, char *cmd )
  60. {
  61.     unsigned int i;
  62.     int match = -1;
  63.     unsigned int argcount;
  64.  
  65.  
  66.     /* Make call "safe" even if RxilInit() failed */
  67.     if( global_rdef->CommandTable == NULL )
  68.     {
  69.         /* This is an unlikely mistake on the client's part, but
  70.          * let's fail gracefully just in case.
  71.          */
  72.         rexxmsg->rm_Result1 = RXERR_UNKNOWN_CMD;
  73.         return;
  74.     }
  75.  
  76.  
  77.     for( i=0; global_rdef->CommandTable[i].Name != NULL; i++ )
  78.     {
  79.         if( global_rdef->CommandTable[i].CaseFlag == TRUE )
  80.         {
  81.             /* Case sensitive */
  82.             if(  strcmp( global_rdef->CommandTable[i].Name, cmd ) == 0  )
  83.             {
  84.                 /* A match */
  85.                 match = i;
  86.                 break;
  87.             }
  88.         }
  89.         else
  90.         {
  91.             /* Not case sensitive */
  92.             if(  stricmp( global_rdef->CommandTable[i].Name, cmd ) == 0  )
  93.             {
  94.                 /* A match */
  95.                 match = i;
  96.                 break;
  97.             }
  98.         }
  99.     }
  100.  
  101.     if( match == -1 )
  102.     {
  103.         /* No match found */
  104.         rexxmsg->rm_Result1 = RXERR_UNKNOWN_CMD;
  105.         return;
  106.     }
  107.  
  108.  
  109.     /* Check privilege level */
  110.     if( global_rdef->CommandTable[match].Privilege >
  111.         global_rdef->FromRexx )
  112.     {
  113.         /* Not from the correct port! */
  114.         rexxmsg->rm_Result1 = RXERR_NOT_A_HARMLESS_CMD;
  115.         return;
  116.     }
  117.  
  118.  
  119.     /* Are we locked?  If so, we respond only to commands
  120.      * received at the secret port.
  121.      */
  122.     if(  ( global_rdef->LockCount != 0 ) &&
  123.         ( global_rdef->FromRexx < RXIL_SECRET )  )
  124.     {
  125.         rexxmsg->rm_Result1 = RXERR_BUSY;
  126.         return;
  127.     }
  128.  
  129. #if 0
  130.     /* Do the argument checking */
  131.     if(  ( global_rdef->CommandTable[match].ArgCount != RXIL_NO_ARGCHECK ) &&
  132.         ( (global_rdef->ArgCount-1) !=
  133.         global_rdef->CommandTable[match].ArgCount )  )
  134.     {
  135.         /* Wrong number of args */
  136.         rexxmsg->rm_Result1 = RXERR_ARG_COUNT;
  137.         return;
  138.     }
  139. #endif
  140.  
  141.  
  142.     /* Do the argument checking */
  143.  
  144.     argcount = global_rdef->ArgCount - 1;    /* This is -1 since ArgCount
  145.                                              * includes the command name
  146.                                              * itself.  This is in keeping
  147.                                              * with the 'C' conventions
  148.                                              * for argc and argv.
  149.                                              */
  150.  
  151.     if( argcount < global_rdef->CommandTable[match].MinArgs )
  152.     {
  153.         /* Wrong number of args */
  154.         rexxmsg->rm_Result1 = RXERR_NO_ARGUMENT;
  155.         return;
  156.     }
  157.  
  158.     if( argcount > global_rdef->CommandTable[match].MaxArgs )
  159.     {
  160.         /* Wrong number of args */
  161.         rexxmsg->rm_Result1 = RXERR_TOO_MANY_ARGS;
  162.         return;
  163.     }
  164.  
  165.  
  166.     /* Call the function. */
  167.     (*(global_rdef->CommandTable[match].Func))(rexxmsg);
  168. }
  169.  
  170.